/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.Game;
import static control.Game.FIELDLIST;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import model.ChessPiece1;
import view.panels.ChessPiecePanel;

/**
 *
 * @author Jonas
 */
public class ChessBoard extends javax.swing.JFrame implements ActionListener {

    private Game game;
    private ChessPiecePanel cpp;
    private Color color;
    private FieldofJustice field;

    /**
     * Creates new form ChessBoard
     */
    public ChessBoard() {
        initComponents();
	game=new Game("Pimp","Slave");
        setSize(425, 450);
        makeOurBoard();
	updatePositions();
    }

    //<editor-fold defaultstate="collapsed" desc="Ini-tilføjelser med skakbræt og brikker">
    private void makeOurBoard() {
        int count = 0;
        int dx = 0;
        int dy = 0;

        for (int i = 0; i < FIELDLIST.length; i++) {
            dy = i * 51;
            for (int j = 0; j < FIELDLIST[i].length; j++) {
                dx = j * 51;
                if (count % 2 == 0) {
                    color = new Color(200, 200, 200);
                    //System.out.println("Tilføjer" + color);
                } else {
                    color = new Color(100, 100, 100);
                    //System.out.println("Tilføjer" + color);
                }
                field = new FieldofJustice(color, new Point(j + 1, i + 1));
                FIELDLIST[i][j] = field;
                field.setBounds(dx, dy, 51, 51);
                jPanel1.add(field);
                field.setVisible(true);
                count++;
            }

            count++;
        }
        jPanel1.repaint();
        revalidate();
        
    }
//</editor-fold>

    private void updatePositions() {
        for (ChessPiece1 cp : game.getPlayer1().getChessPieceList()) {
		cpp=new ChessPiecePanel(cp);
		
		    FIELDLIST[(int)cp.getPosition().getY()-1][(int)cp.getPosition().getX()-1].add(cpp);
		    cpp.setLocation(0, 0);
		    cpp.setVisible(true);
		    FIELDLIST[(int)cp.getPosition().getY()-1][(int)cp.getPosition().getX()-1].repaint();
	}
        for (ChessPiece1 cp : game.getPlayer2().getChessPieceList()) {
		cpp=new ChessPiecePanel(cp);
		
		    FIELDLIST[(int)cp.getPosition().getY()-1][(int)cp.getPosition().getX()-1].add(cpp);
		    cpp.setLocation(0, 0);
		    cpp.setVisible(true);
		    FIELDLIST[(int)cp.getPosition().getY()-1][(int)cp.getPosition().getX()-1].repaint();
	}
	jPanel1.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jPanel1.setBackground(new java.awt.Color(51, 255, 0));
        jPanel1.setLayout(null);
        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 408, 408);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent ae) {
        updatePositions();
    }

}
